import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Packman extends Canvas implements KeyListener, ActionListener, Thing {
	private static final long serialVersionUID = 1L;

	// x-coordinate
	int x = 0;

	// y-coordinate
	int y = 0;

	// Old coordinates for drawing
	int oldX = 0;
	int oldY = 0;

	/*
	 * Describe in which direction packman see: 0-up 1-right 2-down 3-left
	 */
	short direction = 0;

	// Packman mouth opening: open or not
	boolean isMouthOpen;

	// If isMoving = false, packman still stend (no key pressed)
	boolean isMoving = false;

	// Constructor for packman
	public Packman(int i2, int j2) {
		x = i2;
		y = j2;
		oldX = 0;
		oldY = 0;
		direction = 1;
		isMouthOpen = false;
	}

	// Packman moving
	public void move() {
		oldX = x;
		oldY = y;
		if (direction == 0) {
			y--;
		}
		if (direction == 1) {
			x++;
		}
		if (direction == 2) {
			y++;
		}
		if (direction == 3) {
			x--;
		}

	}

	// Packman drawing, when he see right
	public void draw_right(Graphics g, final boolean b) {
		if (b == true) {
			// g.setColor(Color.white);
			// g.fillRect(i * 30, j * 30, 30, 30);
			g.setColor(Color.yellow);
			g.fillArc(x * 30, y * 30, 30, 30, 0, 315);
			g.setColor(Color.black);
			g.fillArc(x * 30 + 20, y * 30 + 5, 3, 3, 0, 360);
		} else {
			// g.setColor(Color.white);
			// g.fillRect(i * 30 + 0, j * 30 + 0, 30, 30);
			g.setColor(Color.yellow);
			g.fillArc(x * 30, y * 30, 30, 30, 0, 360);
			g.setColor(Color.black);
			g.drawArc(x * 30 + 20, y * 30 + 5, 3, 3, 0, 360);
		}
	}

	// Packman drawing, when he see left
	public void draw_left(Graphics g, final boolean b) {
		if (b == true) {
			// g.setColor(Color.white);
			// g.fillRect(i * 30, j * 30, 30, 30);
			g.setColor(Color.yellow);
			g.fillArc(x * 30, y * 30, 30, 30, 170, -300);
			g.setColor(Color.black);
			g.fillArc(x * 30 + 10, y * 30 + 5, 3, 3, 0, 360);
		} else {
			// g.setColor(Color.white);
			// g.fillRect(i * 30 + 0, j * 30 + 0, 30, 30);
			g.setColor(Color.yellow);
			g.fillArc(x * 30, y * 30, 30, 30, 0, 360);
			g.setColor(Color.black);
			g.drawArc(x * 30 + 10, y * 30 + 5, 3, 3, 0, 360);
		}
	}

	// Packman drawing, when he see down
	public void draw_down(Graphics g, final boolean b) {
		if (b == true) {
			// g.setColor(Color.white);
			// g.fillRect(i * 30, j * 30, 30, 30);
			g.setColor(Color.yellow);
			g.fillArc(x * 30, y * 30, 30, 30, 245, -300);
			g.setColor(Color.black);
			g.drawArc(x * 30 + 5, y * 30 + 20, 3, 3, 0, 360);
		} else {
			// g.setColor(Color.white);
			// g.fillRect(i * 30 + 0, j * 30 + 0, 30, 30);
			g.setColor(Color.yellow);
			g.fillArc(x * 30, y * 30, 30, 30, 0, 360);
			g.setColor(Color.black);
			g.fillArc(x * 30 + 5, y * 30 + 20, 3, 3, 0, 360);
		}
	}

	// Packman drawing, when he see up
	public void draw_up(Graphics g, final boolean b) {
		if (b == true) {
			// g.setColor(Color.white);
			// g.fillRect(i * 30, j * 30, 30, 30);
			g.setColor(Color.yellow);
			g.fillArc(x * 30, y * 30, 30, 30, 50, -300);
			g.setColor(Color.black);
			g.drawArc(x * 30 + 5, y * 30 + 10, 3, 3, 0, 360);

		} else {
			// g.setColor(Color.white);
			// g.fillRect(i * 30 + 0, j * 30 + 0, 30, 30);
			g.setColor(Color.yellow);
			g.fillArc(x * 30, y * 30, 30, 30, 0, 360);
			g.setColor(Color.black);
			g.fillArc(x * 30 + 5, y * 30 + 10, 3, 3, 0, 360);
		}
	}	
	
	// Main packman drawing	
	@Override
	public void draw(Graphics g) {		
		g.setColor(Color.white);
		g.fillRect(oldX * 30, oldY * 30, 30, 30);
		g.setColor(Color.black);
		if (direction == 0) {
			draw_up(g, isMouthOpen);
		}
		if (direction == 1) {
			draw_right(g, isMouthOpen);
		}		
		if (direction == 2) {
			draw_down(g, isMouthOpen);
		}
		if (direction == 3) {
			draw_left(g, isMouthOpen);
		}
		super.paint(g);
	}
	
	// What for???
	@Override
	public void hide(){
		
	}

	// Key listeners for packman moving
	@Override
	public void keyReleased(KeyEvent arg0) {
		if (KeyEvent.getKeyText(arg0.getKeyCode()).equalsIgnoreCase("w")) {
			direction = 0;
			isMoving = true;
			oldX = x;
			oldY = y;
		}
		if (KeyEvent.getKeyText(arg0.getKeyCode()).equalsIgnoreCase("s")) {
			direction = 2;
			isMoving = true;
			oldX = x;
			oldY = y;
		}
		if (KeyEvent.getKeyText(arg0.getKeyCode()).equalsIgnoreCase("a")) {
			direction = 3;
			isMoving = true;
			oldX = x;
			oldY = y;
		}
		if (KeyEvent.getKeyText(arg0.getKeyCode()).equalsIgnoreCase("d")) {
			direction = 1;
			isMoving = true;
			oldX = x;
			oldY = y;
		}
	}

	public void keyTyped(KeyEvent e) {
	}

	public void keyPressed(KeyEvent e) {
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		isMouthOpen = !isMouthOpen;
		if (isMoving == true) {
			move();
		}
	}
}
